﻿using MRSite.Common.Dependency;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MRSite.DataAccess
{
    public class DbContextResolve : IDbContextResolve, ISingleton
    {
        public DbContextResolve(IContainerResolver containerResolver, IConnectionStringManager connectStringManager)
        {
            _containerResolver = containerResolver;
            _connectStringManager = connectStringManager;
        }

        public void Realse(object obj)
        {
            _containerResolver.Realse(obj);
        }

        public T Resolver<T>(string connstring) where T : DataContext
        {
            var dbInstance = _connectStringManager.GetDatabaseInstance(connstring);
            if (dbInstance != null && !string.IsNullOrWhiteSpace(dbInstance.ConnectionString))
            {
                return _containerResolver.Resolver<T>(new { connection = dbInstance.ConnectionString });
            }
            return _containerResolver.Resolver<T>();
        }
        private readonly IContainerResolver _containerResolver;
        private readonly IConnectionStringManager _connectStringManager;
    }
}
